﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Caching;
using System.Xml.Linq;
using Netkk.Common;
using Netkk.Common.Interfaces.Repositories;
using Netkk.Common.Models;

namespace Netkk.Data.Repositories
{
    public class DownloadRepository : BaseRepository, IDownloadRepository
    {
        private string downloadPath;
        
        protected XDocument DownloadDocument
        {
            get { return CacheWrapper.Load("download", new CacheDependency(downloadPath), () => XDocument.Load(downloadPath)); }
        }

        public DownloadRepository()
        {
            downloadPath = string.Concat(ConnectionString, "Downloads.xml");
        }

        public IEnumerable<Download> Select()
        {
            IEnumerable<Download> download = from l in DownloadDocument.Element("Downloads").Elements("Download")
                                             select getDownloadFromXElement(l);
            return download;
        }

        private Download getDownloadFromXElement(XElement XDownload)
        {
            return new Download
            {
                Id = XDownload.Element("Id").Value.ToOrDefault<Guid>(),
                Name = XDownload.Element("Name").Value,
                ContentType = XDownload.Element("ContentType").Value,
                ContentSize = XDownload.Element("ContentSize").Value.ToOrDefault<int>(),
                FileName = XDownload.Element("FileName").Value,
                Description = XDownload.Element("Description").Value,
            };
        }

        public void Save(Download download)
        {
            XElement xdownload = (from d in DownloadDocument.Element("Downloads").Elements("Download")
                                  where d.Element("Id").Value.ToOrDefault<Guid>() == download.Id
                                  select d).FirstOrDefault();
            if (xdownload == null)
            {
                xdownload = new XElement("Download");
                DownloadDocument.Element("Downloads").Add(xdownload);
            }
            else
            {
                xdownload.RemoveAll();
            }

            xdownload.Add(
                    new XElement("Id") { Value = download.Id.ToString("B") },
                    new XElement("Name") { Value = download.Name },
                    new XElement("ContentType") { Value = download.ContentType },
                    new XElement("ContentSize") { Value = download.ContentSize.ToString() },
                    new XElement("FileName") { Value = download.FileName },
                    new XElement("Description") { Value = download.Description });
            DownloadDocument.Save(downloadPath);
        }

        public void Delete(Download download)
        {
            XElement xdownload = (from d in DownloadDocument.Element("Downloads").Elements("Download")
                                  where d.Element("Id").Value.ToOrDefault<Guid>() == download.Id
                                  select d).FirstOrDefault();
            if (xdownload != null)
            {
                xdownload.Remove();
                DownloadDocument.Save(downloadPath);
            }
        }

    }

}
